package com.appview;

import com.util.Config;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * Created by Qi_Huang on 2015/11/1.
 * 此类用于教师或学生修改个人信息
 */
public class ModifyInfo extends JFrame implements ActionListener {

    private JLabel[] jLabels;
    private JTextField[] jTextFields;
    private JButton jButton1, jButton2;
    private JPanel[] jPanels;
    private List<String> info;
    private static ModifyInfo modifyInfo;
    private int type;

    public ModifyInfo(int type, List<String> info) {
        this.type = type;
        this.info = info;
        modifyInfo = this;
    }


    public static ModifyInfo getModifyInfo() {
        return modifyInfo;
    }

    public void openView() {
        initComponent();

        for (JPanel jPanel: jPanels) {
            add(jPanel);
        }
        setLayout(new GridLayout(jPanels.length, 1));
        setVisible(true);
        setSize(300, 400);
        setLocation(400, 200);
        setResizable(false);
        setTitle("操作中...");
        setAlwaysOnTop(true);
    }

    private void initComponent() {
        String list1[] = {"工号", "姓名" , "年龄", "性别", "生日", "居住地", "政治面貌", "学院", "专业", "工龄", "职位"};
        String list2[] = {"学号", "姓名", "年龄", "性别", "生日", "居住地","政治面貌", "学院", "专业（班级）" };

        if (Config.getLoginType() == 0) {
            jLabels = new JLabel[11];
            jTextFields = new JTextField[11];
            jPanels = new JPanel[12];
            for (int i = 0; i < list1.length; i++) {
                jLabels[i] = new JLabel(list1[i]);
                jTextFields[i] = new JTextField(10);
            }
            for (int i = 0; i < list1.length + 1; i++) {
                jPanels[i] = new JPanel();
            }
        } else if (Config.getLoginType() == 1) {
            jLabels = new JLabel[9];
            jTextFields = new JTextField[9];
            jPanels = new JPanel[10];
            for (int i = 0; i < list2.length; i++) {
                jLabels[i] = new JLabel(list1[i]);
                jTextFields[i] = new JTextField(10);
            }
            for (int i = 0; i < list2.length + 1; i++) {
                jPanels[i] = new JPanel();
            }
        }

        jButton1 = new JButton("修改");
        for (int i = 0; i < jTextFields.length; i++) {
            jTextFields[i].setText(info.get(i));
        }
        jTextFields[0].setEditable(false);
        jTextFields[1].setEditable(false);

        jButton2 = new JButton("返回");

        jButton1.addActionListener(this);
        jButton2.addActionListener(this);
        for (int i = 0; i < jPanels.length - 1; i++){
            jPanels[i].add(jLabels[i]);
            jPanels[i].add(jTextFields[i]);
            jPanels[i].setLayout(new FlowLayout(FlowLayout.CENTER));
        }
        jPanels[jPanels.length - 1].add(jButton1);
        jPanels[jPanels.length - 1].add(jButton2);
        jPanels[jPanels.length - 1].setLayout(new FlowLayout(FlowLayout.CENTER));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButton1) {
            //修改信息
            TableInfoTools.userChangeInfo(jTextFields);
            this.dispose();
        } else {
            this.dispose();
        }
    }


}
